package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

// 引入dubbo提供的注解，用来发布服务
@Service(timeout = 100000)
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractDao contractDao;

    @Override
    public List<ContractProductVo> findByShipTime(String inputDate) {
        return contractDao.findByShipTime(inputDate);
    }

    @Override
    public PageInfo<Contract> findByPage(
            ContractExample contractExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Contract> list = contractDao.selectByExample(contractExample);
        return new PageInfo<>(list);
    }

    @Override
    public List<Contract> findAll(ContractExample contractExample) {
        return contractDao.selectByExample(contractExample);
    }

    @Override
    public Contract findById(String id) {
        return contractDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Contract contract) {
        //1、设置id
        contract.setId(UUID.randomUUID().toString());
        //2、设置购销合同状态，默认为0表示草稿
        contract.setState(0);
        //3、设置购销合同总金额
        contract.setTotalAmount(0d);// contract.getTotalAmount() + 货物金额
        //4、设置货物数量、附件数量
        contract.setProNum(0);
        contract.setExtNum(0);
        //5、添加
        contractDao.insertSelective(contract);
    }

    @Override
    public void update(Contract contract) {
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        contractDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Contract> findByParentDept(String deptId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Contract> list = contractDao.findByParentDept(deptId);
        return new PageInfo<>(list);
    }
}
